const genomeHeader = {
    "geneid": {"title": "Gene ID", "search": {"types": "text"}, "formatter": "GetGeneId"},
    "chr": {"title": "Chromsome", "search": {"types": "text"}},
    "start": {"title": "Start Position", "search": {"types": "num"}},
    "end": {"title": "End Position", "search": {"types": "num"}},
    "strand": {"title": "Strand", "search": {"types": "text"}},
    "nrname": {"title": "NR_tophit_name", "search": {"types": "text"}},
    "nrdes": {"title": "NR_tophit_description", "search": {"types": "text"}},
    "swissname": {"title": "Swissprot_tophit_name", "search": {"types": "text"}},
    "swissdes": {"title": "Swissprot_tophit_description", "search": {"types": "text"}},
    "go": {"title": "GO", "search": {"types": "text"}, "formatter": "GetGO"},
    "ko": {"title": "KO", "search": {"types": "text"}, "formatter": "GetKegg"},
    "kegg": {"title": "KEGG", "search": {"types": "text"}, "formatter": ""},
    "pfam": {"title": "PFAM", "search": {"types": "text"}, "formatter": "GetPFAM"},
}


const ogHeader = {
    "species": {"title": "Species", "search": {"types": "text"}, "formatter": "GetOg"},
    "accessionId": {"title": "Accession ID", "search": {"types": "text"}},
    "chinese": {"title": "Chinese name", "search": {"types": "text"}},
    "size": {"title": "Size (bp)", "search": {"types": "text"}},
    "conformation": {"title": "Conformation", "search": {"types": "text"}},
    "institution": {"title": "Institution", "search": {"types": "text"}},
    "date": {"title": "Date", "search": {"types": "text"}}
}


function getTableColumns(headJson, table) {
    let columns = [];
    let checkbox = "";
    $.each(headJson, (field, json) => {
        let column = {
            field: field,
            title: json.title,
            sortable: true,
            searchType: json.search.types
        }
        if (json.formatter !== "") {
            column["formatter"] = json.formatter
        }
        columns.push(column)
        checkbox += `<div class="form-check"><label class="form-check-label">
                            <input type='checkbox' checked='checked' value="${field}"  onclick="setColumns('${field}','${table}')"> ${json.title} 
                           <i class="input-helper"></i> </label></div>`
    })
    $(`#${table}-check`).html(checkbox)
    return columns;
}

function GetOg(value, row) {
    return `<a href="/COADB/og/${row.id}/ogInfoPage" target="_blank">${value}</a>`
}

function GetGeneId(value, row) {
    return `<a href="/COADB/browse/genePage/${row.id}" target="_blank">${value}</a>`
}

function GetGeneIdBySelf(value, row) {
    return `<a href="/COADB/browse/genePage/${value}" target="_blank">${value}</a>`
}

function GetGO(value, row) {
    return getResult(value, "http://amigo.geneontology.org/amigo/term/")
}

function GetKegg(value) {
    return getResult(value, "https://www.kegg.jp/dbget-bin/www_bget?ko:")
}

function GetPFAM(value) {
    return  getResult(value,"http://pfam.xfam.org/family/")
}


function setColumns(value,table="table") {
    const element = $(`#${table}-check`).find("input:checkbox[value=" + value + "]");
    if (element.is(":checked")) {
        $('#' + table).bootstrapTable('showColumn', value);
    } else {
        $('#' + table).bootstrapTable('hideColumn', value);
    }
}




function getResult(value, url) {
    if (value === "_" || value === undefined) {
        return "_";
    } else {
        let a = value.split(";");
        let html = "";
        $.each(a, (i, v) => {
            const vv = v.split("::")[0]
            if (url !== "") {
                html += ` <a href="${url}${vv}" target="_blank">${vv}</a> `;
            } else {
                html += ` ${vv}  `;
            }
        })
        return html;
    }
}
